<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>报关行管理 - TMS</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <link rel="stylesheet" href="/view/tms/static/css/iframe.css?v=1.0">
  </head>
  <style>
      .layui-form-item {margin:0;}
      .layui-form-item .layui-inline {margin-bottom:0; margin-right:6px;}
      .layui-form-item .layui-inline .layui-input-inline {margin-right:4px; width:130px;}
      .layui-form-item .layui-inline .layui-form-mid {margin-right:4px;}
      .fold {font-size:23px; font-style:normal; font-weight:bold; cursor:pointer;}
      .sub-table .layui-table-view {margin:3px; margin-left:-1px;}
      .sub-tool-bar {margin:10px 0 12px; text-align:left;}
      searchMore {display:none;}
  </style>
  <body>
    <!--start: 搜索-->
    <div class="panel-handle search-bar">
      <div class="panel-handle search-bar">
        <form class="layui-form layui-form-pane">
          <div class="layui-input-inline">
            <div class="layui-input-inline">
              <input type="text" name="nameShort" placeholder="客户简称" autocomplete="off" class="layui-input">
              <!-- <input type="hidden" name="organizeId"> -->
            </div>
          </div>

          <div class="layui-input-inline">
            <div class="layui-input-inline">
              <input type="text" name="name" placeholder="联系人" autocomplete="off" class="layui-input">
              <!-- <input type="hidden" name="organizeId"> -->
            </div>
          </div>

          <div class="layui-inline">
              <label class="layui-form-label">状态</label>
              <div class="layui-input-inline" style="width:100px">
                <select name="status">
                <option value="" selected="selected">全部</option>
                <option value="1">启用</option>
                <option value="0">停用</option>
                </select>
              </div>
            </div>
          <button class="layui-btn layui-btn-danger" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i>查询</button>
          <button class="layui-btn layui-btn-primary" type="reset" style="margin:0;">重置</button>
        </form>
      </div>
    <!--end: 搜索-->
    <div style="display:none"><a class="tableCB" data-type="customsBrokerAdd" data-func="callbackCB(obj)"; href="javascript:;">增加</a></div>
    <!--start: 按钮组-->
    <div class="panel-handle btns-bar">
      <div class="layui-btn-group fl">
        <button class="layui-btn layui-btn-normal" data-type="addData">新增</button>
        <button class="layui-btn layui-btn-normal" data-type="editData">编辑</button>
        <button class="layui-btn layui-btn-normal" data-type="detailData">查看</button>
        <button class="layui-btn layui-btn-normal" data-type="statusCancle">停用</button>
        <button class="layui-btn layui-btn-normal" data-type="statusStart">启用</button>
      </div>
      <div class="fr">
        <button class="layui-btn"><i class="layui-icon">&#xe62d;</i>导入供应商</button>
      </div>
      <div class="clr"></div>
    </div>
    <!--end: 按钮组-->

    <!--start:表格列表-->
    <table class="layui-hide" id="tableList" lay-filter="tbar"></table>
    <!--end:表格列表-->
    <script type="text/html" id="isDefaultTpl">
      {{#  if(d.hasDefault == 1){ }}
        <a lay-event="statusCancle" style="color:#1E9FFF;">是</a>
      {{#  }else{ }}
        <a lay-event="statusStart" style="color:#FF5722;">否</a>
      {{#  } }}
    </script>
    <script type="text/html" id="statusTpl">
      {{#  if(d.status == 1){ }}
        <a lay-event="statusCancle" style="color:#1E9FFF;">启用</a>
      {{#  }else{ }}
        <a lay-event="statusStart" style="color:#FF5722;">停用</a>
      {{#  } }}
    </script> 
    <!-- <script type="text/html" id="nameCnTpl">
      <a lay-event="detail">{{d.nameCn}}</a>
    </script> -->
  </body>
  <script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
  <script charset="utf-8" src="/view/frame/layui/layui.js"></script>
  <script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>
  
  <script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
  <script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
  <script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
  <script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>
  
  <script>
  layui.use(['form', 'layer', 'table', 'tms_tab','laydate'], function(){
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        tmsTab = layui.tms_tab,
        laydate = layui.laydate,
        $ = layui.jquery;
    //初始化日期时间
    laydate.render({
      elem: '#doarkDateStart'
    });
    laydate.render({
      elem: '#doarkDateEnd'
    });    
    //初始化表格   
    setTableData();
    var $id = $.trim(getUrlParam('id'));
    // console.log($id);
    //监听提交
    form.on('submit(search)', function(data){
      var $nameShort = $.trim(data.field.nameShort),
          $name = $.trim(data.field.name),
          $typeDcode = $.trim(data.field.typeDcode),
          $companyUsedId = $.trim(data.field.typeDcode) ,
          $status = $.trim(data.field.status),
          $parms = {};
          
      // $parms = {
      //   'nameShort' : $nameShort,
      //   'name' : $name,
      //   'status' : $sortName,
      //   'typeDcode' : $typeDcode,
      //   'companyUsedId' : $companyUsedId,
      // }
      var $parms = 'nameShort='+$nameShort+'&name='+$name+'&status='+$status+'&typeDcode='+$typeDcode+'&companyUsedId='+$companyUsedId
      //console.log($parms);
      setTableData($parms);
      return false;
    });
    function callbackCB(objs) {
      if (typeof objs.data === 'object') {
        objs = objs.data
      }
      return objs
    }
    //方法级渲染
    function setTableData($parms,$id){
      var $pageSize = 15;

      table.render({
        elem: '#tableList',
        url: '/ucenter/crm/customers/customersBroker/search.shtml?custId=0',
        // where: $parms,
        response: {
          statusCode: 'SUCCESS',
          countName: 'objects.total', //数据总数的字段名称，默认：count
          dataName: 'objects'
        },
        request: {
          pageName: 'pageNum', //页码的参数名称，默认：page
          limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        cols: [[
          {checkbox: true, fixed: true},
          {field:'coName', title: '报关行名称', width:140,align: 'center'},
          {field:'uname', title: '报关员姓名', width:120, align: 'center'},
          {field:'tel', title: '联系方式', width:150, align: 'center'},
          {field:'address', title: '联系地址', width:190,align: 'center'},
          {field:'hasDefault', title: '是否默认', width:140, templet: '#isDefaultTpl', align: 'center'},         
          {field:'status', title: '状态', width:80, templet: '#statusTpl', align: 'center'}
        ]],
        id: 'dataReload',
        page: false,
        limits: [15, 30, 50, 100],
        limit: $pageSize,
        height: 'full-130',
        even: true
      });
    }

    //监听每一行的事件处理
    // table.on('tool(tbar)', function(obj){
    //   var data = obj.data;
    //   console.log(JSON.stringify(data));
    //   if(obj.event === 'detail'){
    //     //事件
    //   }
    // });

    var active = {
      //新增
      addData: function(){
        //第一种方式：tabs选项卡
        //tmsTab.add($(this), '新增供应商', 'modify.html?do=add');
        window.customerAdd = function(){
          var cllBack = $('.tableCB').attr('data-func')
              var Fun = eval(cllBack);
              var objs = Fun;
            var reb = false;
            if(objs.conAdd == 1){
              reb = true;
            } 
            console.log(objs);
            $.ajax({             
              type:'post', 
              url:'/ucenter/crm/customers/customersBroker/add.shtml?id=0', 
              dataType:"json",      
              contentType:"application/json",               
              data:JSON.stringify(objs), 
              success:function(d){
                console.log(d);
                var $code = d.code,
                    $msg = d.msg,
                    $objects = d.objects;
                    
                if($code === 'SUCCESS'){
                  // parent.layer.alert('保存成功！', {
                    // yes: function(){
                      //关闭窗口
                      parent.layer.closeAll();
                      //获取当前框架ID并刷新
                      var $layId = $(window.parent.document).find('.layui-this').attr('lay-id');
                      parent['f' + $layId].location.reload();
                    // }
                  // });
                }
              },
            });
        };
        //第二种方式：弹出窗口
        parent.layer.open({
          type: 2,
          title: '新增报关行',
          shadeClose: true,
          shade: 0.8,
          area: ['80%', '80%'],
          content: './customerLinkerTpl.html?do=add&' + Math.random(),
          
          btn: ['确定','关闭'],
          btn1: function(index,layero){
              $(layero.find('iframe')[0].contentWindow.document).find('#vsubmit').click();            
              
          }
        });
      },
      //编辑
      editData: function(){ //获取选中数据
        var checkStatus = table.checkStatus('dataReload'),
            data = checkStatus.data[0];
            console.log(data[0]);
            window.customerEdit = function(){
              var cllBack = $('.tableCB').attr('data-func')
              var Fun = eval(cllBack);
              var objs = Fun;
              // Fun;
              delete objs.$id;
              delete objs.conAdd;
              objs['id'] = data.id;
              console.log(objs);
              // console.log($id);
                      //保存数据，调用接口
              $.ajax({
                type:'PUT', 
                url:'/ucenter/crm/customers/customersBroker/0.shtml', 
                dataType:"json",      
                contentType:"application/json",               
                data:JSON.stringify(objs), 
                success:function(d){
                  console.log(d);
                  var $code = d.code,
                      $msg = d.msg,
                      $objects = d.objects;
                      
                  if($code === 'SUCCESS'){
                    // obj.update(objs);
                    parent.layer.closeAll();
                    parent.layer.alert('保存成功！', {
                      yes: function(){
                        //关闭窗口
                        parent.layer.closeAll();
                        
                        //获取当前框架ID并刷新
                        
                        var $layId = $(window.parent.document).find('.layui-this').attr('lay-id');
                        parent['f' + $layId].location.reload();
                      }
                    });
                  }
                }
              });
            }
        if(data.length == 0){
          parent.layer.alert('你还未选中数据！');
        }else if(data.length > 1){
          parent.layer.alert('只能选择一条数据！');
        }else{
          // tmsTab.add($(this), '编辑价格协议（' + data[0].targetName + '）', 'modify.html?do=edit&id=' + data[0].id+'&targetType='+data[0].targetType);
          parent.layer.open({
            type: 2,
            title: '编辑报关行',
            shadeClose: true,
            shade: 0.8,
            area: ['80%', '80%'],
            content: './customerLinkerTpl.html?do=edit&idD='+data.id,
            success: function(layero, index){
              // for (var key in data) {
              //   data[key] != null ? data[key] : '';
              // }
              // console.log($(layero.find('iframe')[0].contentWindow.document).find('input'));
              // var inputLists = $(layero.find('iframe')[0].contentWindow.document).find('input')
              // for(var j = 0; j< inputLists.length; j++){  
              //       $(inputLists[j]).val(data[inputLists[j]['name']]);
              // }
            },
            btn: ['确定','关闭'],
            yes: function(index,layero){
              $(layero.find('iframe')[0].contentWindow.document).find('#vsubmit').click();

              
              // obj.update(objs);
            }
        });
        }
      },
      //查看
      detailData: function(){
        var checkStatus = table.checkStatus('dataReload'),
            data = checkStatus.data[0];
            console.log(data);
        if(data.length == 0){
          parent.layer.alert('你还未选中数据！');
        }else if(data.length > 1){
          parent.layer.alert('只能选择一条数据！');
        }else{
          parent.layer.open({
              type: 2,
              title: '查看装卸单位',
              shadeClose: true,
              shade: 0.8,
              area: ['80%', '80%'],
              content: './customerLinkerTpl.html?do=detail&idD='+data.id+'&addressId='+data.addressAreaId,
          });
        // }
        }
      },
      //停用
      statusCancle: function(){
        var checkStatus = table.checkStatus('dataReload'),
            data = checkStatus.data;
        if(data.length == 0){
          parent.layer.alert('你还未选中数据！');
        }else{
          parent.layer.confirm('您确定要停用供应商吗？', function(index){
            var $ids = [];
            for(var $i = 0; $i < data.length; $i++){
              if(data[$i].status == 3){
                parent.layer.alert(data[$i].name+'已经是停用状态！');
                return;
              }
              $ids.push(data[$i].id);
            }
            roleStatus($ids, 3);
            parent.layer.close(index);
          });
        }
      },
      //启用
      statusStart: function(){
        var checkStatus = table.checkStatus('dataReload'),
            data = checkStatus.data;
        if(data.length == 0){
          parent.layer.alert('你还未选中数据！');
        }else{
          parent.layer.confirm('您确定要启用供应商吗？', function(index){
            var $ids = [];
            for(var $i = 0; $i < data.length; $i++){
              if(data[$i].status == 1){
                parent.layer.alert(data[$i].name+'已经是启用状态！');
                return;
              }
              $ids.push(data[$i].id);
            }
            roleStatus($ids, 1);
            parent.layer.close(index);
          });
        }
      }
    };
  
    $('.btns-bar .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //改变状态，1启用，2禁用
    function roleStatus($ids, $status){
      var $saveData = {
        ids: $ids,
        status: $status
      }

      $.ajax({
        type:'post', 
        url:'/ucenter/crm/customers/customersBroker/use.shtml', 
        dataType:"json",      
        contentType:"application/json",               
        data:JSON.stringify($saveData), 
        success:function(d){
          var $code = d.code,
              $msg = d.msg,
              $objects = d.objects;
              
          if($code === 'SUCCESS'){
            window.location.reload();
          }else{
            parent.layer.alert('操作失败！');
          }
        }
      });
    }
  });
  </script>
</html>